package utils

import (
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
	databases "house/database"
	"math/rand"
	"net/http"
	"time"
)

func SendCode(account string) error {

	code := fmt.Sprintf("%04v", rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(10000))

	//短信宝
	apiurl := beego.AppConfig.DefaultString("sms::apiurl", "")
	apiaName := beego.AppConfig.DefaultString("sms::apiaName", "")
	apiPwd := beego.AppConfig.DefaultString("sms::apiPwd", "")
	content := "您的验证码是" + code
	url := apiurl + "sms?u=" + apiaName + "&p=" + apiPwd + "&m=" + account + "&c=" + content
	//验证是否成功
	_, err := http.Get(url)
	if err != nil {
		return err
	}

	err = databases.RedisCLi.Set(account, code, 0).Err()
	if err != nil {
		panic(err)
	}
	return nil
}
